<template>
  <div class="tags">
    <!-- tags标签页 -->
    <el-tag
      :key="tag.name"
      v-for="(tag, index) in tags"
      :closable="tag.name !== 'Home'"
      :disable-transitions="false"
      @close="handleClose(tag, index)"
      @click="changeMenu(tag)"
      :effect="$route.name === tag.name ? 'dark' : 'plain'"
    >
      {{ tag.label }}
    </el-tag>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "CommonTags",
  computed: {
    //获取vuex的标签集合数据
    ...mapState({
      tags: state => state.tab.tabsList
    })
  },
  methods: {
    ...mapMutations({
      close: "closeTab"
    }),
    //选择标签跳转路由
    changeMenu(tag) {
      this.$store.commit("selectMenu", tag);
      this.$router.push({ name: tag.name });
    },
    //关闭标签
    handleClose(tag, index) {
      let length = this.tags.length - 1;
      this.close(tag);
      if (tag.name !== this.$route.name) {
        return;
      }
      if (index === length) {
        this.$router.push({ name: this.tags[index - 1].name });
      } else {
        this.$router.push({ name: this.tags[index].name });
      }
    }
  }
};
</script>
<style scoped>
.el-tag {
  margin: 1px;
}
.tags {
  display: flex;
}
</style>
